<?php
/**
 * EverBox functions and definitions
 *
 * @package EverBox
 */
define('EVERBOX_URL', get_template_directory_uri());
define('EVERBOX_DIR', get_template_directory());


// applied when calling get_tag_link()
add_filter('tag_link', 'ludou_html_tag_link', 10, 2);

/**
 * tag页链接转换，将 /tag/tag-name/ 转成 /tag/tag-name.html
 */
function ludou_html_tag_link($tag_link, $tag_id) {
    return rtrim($tag_link, '/') . '.html';
}

// applied when calling get_category_link()
add_filter('category_link', 'ludou_html_cate_link', 10, 2);

/**
 * category页链接转换，
 */
function ludou_html_cate_link($tag_link, $tag_id) {
    return rtrim($tag_link, '/') . '.html';
}

// applied when calling get_category_link()
add_filter('author_link', '_html_author_link', 10, 2);

/**
 * category页链接转换，
 */
function _html_author_link($tag_link, $tag_id) {
    ///var_dump($tag_link);
    return rtrim($tag_link, '/') . '.html';
}

//get_comment_link.文章归档优化
add_filter('month_link', '_get_archives_link', 10, 2);
function _get_archives_link($tag_link, $tag_id) {
   //var_dump($tag_link);
    return rtrim($tag_link, '/') . '.html';
}


//分页连接URL 优化
add_filter('get_pagenum_link','_get_pagenum_link',10,1);
function _get_pagenum_link($page_url){
    if($page_url == get_bloginfo('url').'/' || isset($_GET['s'])){
        return $page_url;
    }
    $page_url = str_replace('.html', '', $page_url);

    return rtrim($page_url,'/').'.html';
}
remove_action('template_redirect', 'redirect_canonical');


add_filter('preview_post_link','_preview_post_link');
function _preview_post_link($url){
    //var_dump(get_bloginfo('url'));//http://blog.a.com:88/?p=87&preview=true"
    $url = str_replace(get_bloginfo('url').'/?', get_bloginfo('url').'/index.php?', $url);

    return $url;
}

/* =Theme Core
----------------------------------*/
require EVERBOX_DIR . '/inc/theme-setup.php';
require EVERBOX_DIR . '/inc/template-tags.php';
require EVERBOX_DIR . '/inc/jetpack.php';
require EVERBOX_DIR . '/inc/widgets/class-popular-widget.php';
require EVERBOX_DIR . '/inc/customizer.php';
require EVERBOX_DIR . '/inc/extras.php';

/**
 * 分页代码函数
 * @global int $paged
 * @global type $wp_query
 * @global type $max_page
 * @param type $range
 */
function par_pagenavi($range = 9){
	global $paged, $wp_query ,$max_page;

	if ( !$max_page ) {$max_page = $wp_query->max_num_pages;}
	if($max_page > 1){if(!$paged){$paged = 1;}
	if($paged != 1){echo "<a href='" . get_pagenum_link(1) . "' class='extend' title='跳转到首页'> 返回首页 </a>";}
	previous_posts_link(' 上一页 ');
        //var_dump($wp_query->query_vars['posts_per_page']);
        if($max_page > $range){
		if($paged < $range){
                    for($i = 1; $i <= ($range + 1); $i++){
                        echo "<a href='" . get_pagenum_link($i) ."'";
                        if($i==$paged)
                            echo " class='current'";echo ">$i</a>";
                    }
                }elseif($paged >= ($max_page - ceil(($range/2)))){
                    for($i = $max_page - $range; $i <= $max_page; $i++){
                        echo "<a href='" . get_pagenum_link($i) ."'";
                        if($i==$paged)
                            echo " class='current'";echo ">$i</a>";
                    }
                }elseif($paged >= $range && $paged < ($max_page - ceil(($range/2)))){
                    for($i = ($paged - ceil($range/2)); $i <= ($paged + ceil(($range/2))); $i++){
                        echo "<a href='" . get_pagenum_link($i) ."'";
                        if($i==$paged)
                            echo " class='current'";
                        echo ">$i</a>";
                    }
                }
        }else{
            for($i = 1; $i <= $max_page; $i++){
                echo "<a href='" . get_pagenum_link($i) ."'";
                if($i==$paged)
                    echo " class='current'";
                echo ">$i</a>";
            }
        }
	next_posts_link(' 下一页 ');
    //if($paged != $max_page){echo "<a href='" . get_pagenum_link($max_page) . "' class='extend' title='跳转到最后一页'> 最后一页 </a>";}

    }
}

/* 访问计数 */
function record_visitors(){
	if (is_singular())
	{
	  global $post;
	  $post_ID = $post->ID;
	  if($post_ID)
	  {
		  $post_views = (int)get_post_meta($post_ID, 'views', true);
		  if(!update_post_meta($post_ID, 'views', ($post_views+1)))
		  {
			add_post_meta($post_ID, 'views', 1, true);
		  }
	  }
	}
}
add_action('wp_head', 'record_visitors');

/// 函数名称：post_views
/// 函数作用：取得文章的阅读次数
function post_views($before = '(点击 ', $after = ' 次)', $echo = 1){
  global $post;
  $post_ID = $post->ID;
  $views = (int)get_post_meta($post_ID, 'views', true);
  if ($echo) echo $before, number_format($views), $after;
  else return $views;
}


function get_content_relation_cates(){
    global $post;
    $cats = wp_get_post_categories($post->ID);
    echo '<div class="relation-article"><span>相关文章:</span><ul>';
   if ($cats){
            $args = array(
                  'category__in' => array( $cats[0] ),
                  'post__not_in' => array( $post->ID ),
                  'showposts' => 6,
                  'ignore_sticky_posts' => 1
              );
        query_posts($args);

        if (have_posts()) {
            $str = '';
            while (have_posts()) {
              the_post();update_post_caches($posts);
              $url = get_the_permalink($posts);
              $title = get_the_title($posts);
              $str .= "<li><a href=\"$url\"  title=\"$title\" >$title</a></li>";
            }
        }else {
            $str = '<li>暂无相关文章</li>';
        }
        wp_reset_query();
    }else {
      $str = '<li>暂无相关文章</li>';
    }
    echo $str.'</ul></div>';
}




/**
 *  添加一个小工具
 */
function coolwp_remove_meta_widget() {
    /*移除Wordpress自带的Meta小工具*/
    unregister_widget('WP_Widget_Meta');
    /*注册自己的Meta小工具*/
    register_widget('WP_Widget_Author_Inter');
}
add_action( 'widgets_init', 'coolwp_remove_meta_widget' );
/*
自定义小工具扩展类
 */
class WP_Widget_Author_Inter extends WP_Widget {
    function __construct() {
        $widget_ops = array('classname' => 'autor inter', 'description' => __( "the author of this blog,inter myself!") );
        parent::__construct('meta', __('Meta'), $widget_ops);
    }
    function widget( $args, $instance ) {
        extract($args);
        $title = apply_filters('widget_title',empty($instance['title']) ? __('Meta') : $instance['title'], $instance, $this->id_base);
        $inter = apply_filters('widget_inter',empty($instance['inter']) ? __('inter') : $instance['inter'], $instance, $this->id_base);

        echo $before_widget;
        if ( $title ){
            echo $before_title . $title . $after_title;
        }

        echo "<div id='author-blog-inter'><div class='top'><span class='img' >"
        . "<img src='http://www.gangguo.net/wp-content/uploads/2016/12/admin.jpg'></span>".
                "<span class='info'>".$inter
                . " [<a class='a-link' href=".esc_url(home_url('/')).">".get_bloginfo('name')."</a>]</span></div>".
                "<!--span>".wp_register('','',FALSE).",". wp_loginout('',FALSE)."</span-->"
                . "<span class='tolk'><ul><li><img src='http://www.gangguo.net/wp-content/uploads/2016/12/weixin-blog.png'><a href='#' ><i class='fa fa-weixin'></i></a></li>"
                . "<li><a target='_blank' href='http://wpa.qq.com/msgrd?v=3&uin=549224868&site=qq&menu=yes'><i class='fa fa-qq'></i></a></li>"
                . "<li><a href='Mailto:gangguoer@126.com?Subject=Hello World&Body=你好' ><i class='fa fa-envelope-o'></i></a></li></ul></span></div>";
        echo $after_widget;
    }
    function update( $new_instance, $old_instance ) {
        $instance = $old_instance;
        $instance['title'] = strip_tags($new_instance['title']);
        $instance['inter'] = strip_tags($new_instance['inter']);
        return $instance;
    }
    function form( $instance ) {
        $instance = wp_parse_args((array) $instance, array('title' => '','inter'=>''));
        $title = strip_tags($instance['title']);
        $inter = strip_tags($instance['inter']);
        ?>
        <p>
            <label for="<?php echo $this->get_field_id('title'); ?>">
                <?php _e('Title:'); ?>
            </label>
            <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($title); ?>" />
            <label for="<?php echo $this->get_field_id('inter'); ?>">
                简介:
            </label>
            <textarea class="widefat" rows="8" id="<?php echo $this->get_field_id('inter'); ?>" name="<?php echo $this->get_field_name('inter'); ?>" ><?php echo esc_attr($inter); ?></textarea>
        </p>
        <?php
    }
}




/**
 *  添加一个小工具  player
 */
function player_remove_meta_widget(){
    /*注册自己的Meta小工具*/
    register_widget('WP_Widget_Author_Player');

}
add_action('widgets_init', 'player_remove_meta_widget' );
/*
自定义小工具扩展类
 */
class WP_Widget_Author_Player extends WP_Widget {
    function __construct(){
        parent::__construct(
            // 小工具ID
                'tutsplus_list_pages_widget',

                // 小工具名称
                __('mp3播放器','mp3Player' ),

                // 小工具选项
                array (
                    'description' => __('这是一款非常精巧的jQuery音乐播放器插件jPlayer，这款jQuery音乐播放器的外观非常迷人', 'mp3Player' )
                )
            );


    }
    function widget( $args, $instance ) {
        extract($args);
        $title = apply_filters('widget_title',empty($instance['title']) ? __('Meta') : $instance['title'], $instance, $this->id_base);


        echo $before_widget;
        if ( $title ){
            echo $before_title . $title . $after_title;
        }
echo <<<Eof
<iframe id="wp-a-player" frameborder="no" border="0" marginwidth="0" marginheight="0"
    width=330 height=450 src="//music.163.com/outchain/player?type=0&id=550253424&auto=0&height=430"></iframe>
   <script>$(function(){
       $('#wp-a-player').load(function(){
               //var aa= $(this).contents().find("#player").html();

           });
   });</script>
Eof;
        echo $after_widget;
    }
    function update( $new_instance, $old_instance ) {
        $instance = $old_instance;
        $instance['title'] = strip_tags($new_instance['title']);
        return $instance;
    }
    function form( $instance ) {
        $instance = wp_parse_args((array) $instance, array('title' => ''));
        $title = strip_tags($instance['title']);
        ?>
        <p>
            <label for="<?php echo $this->get_field_id('title'); ?>">
                <?php _e('Title:'); ?>
            </label>
            <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($title); ?>" />
        </p>
        <?php
    }
}

?>
